package com.tulun.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * desc: 时间工具类
 * @user:gongdezhe
 * @date:2018/8/19
 */

public class DateUtils {
    private static ThreadLocal<SimpleDateFormat> DATE_FORMAT_LOCAL = new ThreadLocal<SimpleDateFormat>();
    private static ThreadLocal<SimpleDateFormat> DATE_TIME_FORMAT_LOCAL = new ThreadLocal<SimpleDateFormat>();

    /**
     * 获取SimpleDateFormat，格式化时间为：yyyy-MM-dd
     * @return SimpleDateFormat
     */
    public static SimpleDateFormat getDateFormat() {
        SimpleDateFormat df = DATE_FORMAT_LOCAL.get();
        if (df == null) {
            df = new SimpleDateFormat("yyyy-MM-dd");
            DATE_FORMAT_LOCAL.set(df);
        }
        return df;
    }

    /**
     * 获取SimpleDateFormat，格式化时间为：yyyy-MM-dd HH:mm:ss
     * @return SimpleDateFormat
     */
    public static SimpleDateFormat getDateTimeFormat() {
        SimpleDateFormat df = DATE_TIME_FORMAT_LOCAL.get();
        if (df == null) {
            df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            DATE_TIME_FORMAT_LOCAL.set(df);
        }
        return df;
    }

    /**
     * 时间格式化，格式：2011-11-11 11:11:11
     * @return String
     */
    public static String getDateTime() {
        return getDateTime(new Date());
    }

    /**
     * 时间格式化
     * @param date- 时间对象
     * @return String
     */
    public static String getDateTime(Date date) {
        if (date == null)
            return "";
        return getDateTimeFormat().format(date);
    }
}
